package main

import (
	"encoding/xml"
	"fmt"
	"io"
	"os"
	"strings"
)

func main() {
	dec := xml.NewDecoder(os.Stdin)
	var stack []string
	for {
		tok, err := dec.Token()
		if err == io.EOF {
			break
		} else if err != nil {
			fmt.Fprintf(os.Stderr, "xmlselect: %v\n", err)
			os.Exit(1)
		}

		switch tok := tok.(type) {
		case xml.StartElement:
			stack = append(stack, tok.Name.Local)
		case xml.EndElement:
			stack = stack[:len(stack)-1]
		case xml.CharData:
			if containsAll(stack, os.Args[1:]) {
				fmt.Printf("%s: %s\n",
					strings.Join(stack, " "),
					tok)
			}
		}
	}
}

func containsAll(a, b []string) bool {
	for len(a) >= len(b) {
		if len(b) == 0 {
			return true
		}
		if a[0] == b[0] {
			a, b = a[1:], b[1:]
		} else {
			a = a[1:]
		}
	}
	return false
}
